<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="工厂">
          <el-select
            v-model="searchParam.plantId"
            placeholder="请选择工厂"
            clearable
            @keyup.enter="search"
          >
            <el-option
              v-for="item in plantList"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="承运商">
          <el-select
            v-model="searchParam.generalCarrierId"
            placeholder="请选择承运商"
            clearable
            @keyup.enter="search"
          >
            <el-option
              v-for="item in generalCarrierList"
              :key="item.id"
              :label="item.generalCarrierCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="发货时间">
          <el-date-picker
            v-model="searchParam.dateSearch"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            @change="getplanDate"
          />
        </el-form-item>
        <el-form-item label="送货单号">
          <el-input
            v-model="searchParam.deliveryCode"
            placeholder="请输入送货单号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
          <el-button
            class="ml-3"
            @click="
              useDownload(
                '/srm/srmexteriordelivery/export',
                '外协送货单',
                searchParam
              )
            "
          >
            <template #icon><i-ep-download /></template>导出</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <!--        <el-button type="success" plain @click="openDialog()">-->
        <!--          <i-ep-plus/>-->
        <!--          新增-->
        <!--        </el-button>-->
        <!--        <el-button type="danger" plain :disabled="ids.length === 0" @click="handleTableDelete()">-->
        <!--          <i-ep-delete/>-->
        <!--          删除-->
        <!--        </el-button>-->
      </template>

      <el-table
        ref="dataT ableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <!--        <el-table-column type="selection" width="55" align="center"/>-->
        <el-table-column
          label="送货单号"
          prop="deliveryCode"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="送货类型"
          prop="exteriorDeliveryType"
          min-width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{
              useDictionary("DeliveryOrderType", scope.row.exteriorDeliveryType)
            }}
          </template>
        </el-table-column>
        <!--        <el-table-column label="公司ID;基本信息" prop="organizationId" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="公司代码;基本信息" prop="organizationCode" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="公司名称;基本信息" prop="organizationName" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="工厂ID;外向交货" prop="plantId" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="工厂编码" prop="plantCode" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <el-table-column
          label="工厂"
          prop="plantCode"
          width="100"
          show-overflow-tooltip
        />
        <!--        <el-table-column label="供应商id;内向交货" prop="supplierId" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="供应商编号" prop="supplierCode" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="供应商名称" prop="supplierName" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="交货地点id;内向交货" prop="storageLocationId" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="交货地点编号" prop="storageLocationCode" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="交货地点名称" prop="storageLocationName" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="预计到货日期" prop="expectedDeliveryDate" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="到货时间窗开始时间" prop="arriveTimeWindowBegin" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="到货时间窗结束时间" prop="arriveTimeWindowEnd" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="到货时间窗编号" prop="arriveTimeWindowCode" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <el-table-column
          label="承运商"
          prop="generalCarrierCode"
          min-width="100"
          show-overflow-tooltip
        />
        <!--        <el-table-column label="发货联系人" prop="shippingContactPerson" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="发货联系人电话" prop="shippingContactPhone" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <el-table-column
          label="车牌号"
          prop="vehicleCode"
          min-width="100"
          show-overflow-tooltip
        />
        <!--        <el-table-column label="车辆类型id" prop="vehicleTypeId" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <!--        <el-table-column label="车辆类型编号" prop="vehicleTypeCode" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <el-table-column
          label="车辆类型"
          prop="vehicleTypeName"
          min-width="100"
          show-overflow-tooltip
        />
        <!--        <el-table-column label="车辆行驶证号" prop="vehicleRegistration" min-width="100" show-overflow-tooltip>-->
        <!--        </el-table-column>-->
        <el-table-column
          label="司机"
          prop="driverName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="联系电话"
          prop="driverPhone"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="司机驾驶证号"
          prop="driverRegistration"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="发货状态"
          prop="shipmentStatus"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.shipmentStatus === 1"
              >待发货</el-tag
            >
            <el-tag v-if="scope.row.shipmentStatus === 2">已发货</el-tag>
            <el-tag type="success" v-if="scope.row.shipmentStatus === 3"
              >已发货</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          label="发货时间"
          prop="deliveryTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="收货状态"
          prop="receiveStatus"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <el-tag type="info" v-if="scope.row.receiveStatus === 0"
              >未收货</el-tag
            >
            <el-tag v-if="scope.row.receiveStatus === 1">部分收货</el-tag>
            <el-tag type="success" v-if="scope.row.receiveStatus === 2"
              >已收货</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="80" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
            >
              <i-ep-search />
              查看
            </el-button>

            <!--            <el-button-->
            <!--                v-if="scope.row.shipmentStatus == 1"-->
            <!--                type="primary"-->
            <!--                size="small"-->
            <!--                link-->
            <!--                @click="openDialog(scope.row)">-->
            <!--              <i-ep-edit/>-->
            <!--              编辑-->
            <!--            </el-button>-->

            <!--            <el-button v-if="scope.row.shipmentStatus == 2" type="primary" size="small" link-->
            <!--                       @click="sendProduct(scope.row)">-->
            <!--              <i-ep-edit/>-->
            <!--              到达-->
            <!--            </el-button>-->

            <!--            <el-button-->
            <!--                v-if="scope.row.shipmentStatus == 1"-->
            <!--                type="primary"-->
            <!--                size="small"-->
            <!--                link-->
            <!--                @click="handleTableDelete(scope.row.id)">-->
            <!--              <i-ep-edit/>-->
            <!--              删除-->
            <!--            </el-button>-->
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <srmexteriordelivery-edit
      :key="addKey"
      ref="srmexteriordeliveryEditRef"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDictionary, useDownload } from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "PARTNERSRMEXTERIORDELIVERY",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/srm/srmexteriordelivery/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {};

const deleteParam = {
  api: "/srm/srmexteriordelivery/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlantList();
  getGeneralCarrierList();
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/srm/srmexteriordelivery/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开外协送货单表单弹窗 */
const addKey = ref(0);
const srmexteriordeliveryEditRef = ref();

function openDialog(roleObj?: Object) {
  addKey.value++;
  if (roleObj) {
    addDialog.title = "修改外协送货单";
    getInfo(roleObj.id);
    nextTick(() => {
      srmexteriordeliveryEditRef.value.init(roleObj.id);
    });
  } else {
    addDialog.title = "新增外协送货单";
    addDialog.visible = true;
    nextTick(() => {
      srmexteriordeliveryEditRef.value.init("");
    });
  }
  addDialog.readvisible = false;
}

/**
 * 发货
 * */
function sendProduct(data: any) {
  request({
    url: `/srm/srmexteriordelivery/arrive`,
    method: "post",
    data: data,
  })
    .then((data) => {
      search();
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开外协送货单表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "外协送货单详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
  nextTick(() => {
    srmexteriordeliveryEditRef.value.init(roleObj.id);
  });
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}

//工厂列表
const plantList = ref([]);
const getPlantList = async () => {
  const { data } = await request({
    url: "system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plantList.value = data;
  searchParam.value.plantId = plantList.value[0].id;
};

//工厂列表
const generalCarrierList = ref([]);
const getGeneralCarrierList = async () => {
  const { data } = await request({
    url: "/mdm/mdmgeneralcarrier/getGeneralCarrierList",
    method: "post",
    data: {
      enabled: 1,
    },
  });
  generalCarrierList.value = data;
};

function getplanDate(data: any) {
  if (data) {
    searchParam.value.dateStart = data[0];
    searchParam.value.dateEnd = data[1];
  } else {
    searchParam.value.dateStart = "";
    searchParam.value.dateEnd = "";
  }
}
</script>
<style lang="scss" scoped>
:deep(.el-form .el-form-item__label) {
  width: 63px;
}
</style>
